package com.chen.androidstudy.utils

import com.base.library.utils.DeviceUtils
import com.base.library.utils.SPUtils
import java.util.regex.Pattern


object CommonUtil {
    private var hasAgreePrivacy = false

    fun getUniqueDeviceId(): String? {
        if (!hasAgreePrivacy) {
            hasAgreePrivacy = SPUtils.getInstance().getBoolean(SPUtils.KEY_PRIVACY_AGREE)
        }
        if (hasAgreePrivacy) {
            return DeviceUtils.getUniqueDeviceId()
        }
        return null
    }

    fun getInputString(count: String?, default: String): String {
        var startStr = default
        if ("0" == default){
            startStr = ""
        }
        var sb = StringBuilder(startStr)
        when (count) {
            "." -> {
                if (sb.isEmpty()) {
                    sb.append("0.")
                } else {
                    sb.append(".")
                }
            }
            else -> {
                if ("00" == count) {
                    if (sb.isNotEmpty()) {
                        sb.append("00")
                    }
                } else if ("0" == count) {
                    if (sb.isNotEmpty()) {
                        sb.append("0")
                    }
                } else {
                    sb.append(count)
                }
            }
        }
        return sb.toString()
    }

    fun getIsGoodsCode(keyContent: String): Boolean {
        if (keyContent.startsWith("69")) {
            return true
        }
        return false
    }

    fun getIsPhonePay(keyContent: String): Boolean {
        var pattern = Pattern.compile("^1[0-5]\\d{16}\$") //微信 18位纯数字，以10、11、12、13、14、15开头
        var matcher = pattern.matcher(keyContent)
        if (matcher.matches()) {
            return true
        }
        pattern = Pattern.compile("^(?:2[5-9]|30)\\d{14,24}\$") //支付宝用户付款码，25-30 开头的长度为 16-24 位的数字，实际字符串长度以开发者获取的付款码长度为准；付款码使用一次即失效
        matcher = pattern.matcher(keyContent)
        if (matcher.matches()) {
            return true
        }
        pattern = Pattern.compile("^88\\d{19}\$") //职工卡二维码
        matcher = pattern.matcher(keyContent)
        if (matcher.matches()) {
            return true
        }
        return false
    }
}
